home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / triplas / triplas.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-01  |  3.7 KB  |  141 lines

  1. /*    --- triplas.cpp ---
  2.   A variation on the original plasma algorithm by Bret Mulvey.
  3.   Copyright (c) 1995 David Johnston
  4.  
  5.   Last modified March 1st, 1995.
  6.  
  7.     The Story: I got sick and tired of the plain old plasma, because it
  8.   always introduced artifacts into its creation in the form of virticle
  9.   and horizontal definition lines.  This was especially apparent when
  10.   the plasma screen was projected three dimentionally.  So I decided to
  11.   write a plasma based on a triangle, in stead of the traditional
  12.   rectangle.  Here is the end product.  I hope you like it :)
  13.  
  14.     This program was written for Turbo C++ v3.0.  With little modification
  15.   it should compile all right under other compilers.  It should compile
  16.   flawlessly under Borland C++.
  17. */
  18.  
  19. #include <dos.h>
  20. #include <stdlib.h>
  21. #include <conio.h>
  22. #include <math.h>
  23. #include <mem.h>
  24.  
  25. #define INITWIDTH 300
  26. #define INITHEIGHT 199
  27. #define F 2.0
  28. #define xadj (width/2)
  29. #define yadj (height/2)
  30. #define round(q) ((long)(q)+0.5)
  31.  
  32. REGPACK r;
  33. unsigned char far *veep=(unsigned char far *)MK_FP(0xa000,0);
  34.  
  35. //----------------------------------------------------------------------------
  36.  
  37. void setpalette(unsigned char *p)
  38. {
  39.   unsigned o=FP_OFF(p),s=FP_SEG(p);
  40.   r.r_ax=0x1012;
  41.   r.r_bx=0;
  42.   r.r_cx=256;
  43.   r.r_dx=o;
  44.   r.r_es=s;
  45.   intr(0x10,&r);
  46. }
  47.  
  48. //----------------------------------------------------------------------------
  49.  
  50. inline unsigned char getpixel(int x, int y)
  51. {
  52.   return veep[x+y*320];
  53. }
  54.  
  55. //----------------------------------------------------------------------------
  56.  
  57. inline void setpixel(int x, int y,unsigned char c)
  58. {
  59.   if(!getpixel(x,y))
  60.     veep[x+y*320]=c;
  61. }
  62.  
  63. //----------------------------------------------------------------------------
  64.  
  65. inline void adjust(float x1,float y1,float x2,float y2)
  66. {
  67.   float color,c;
  68.   int a,b;
  69.   a=getpixel(round(x1),round(y1));
  70.   b=getpixel(round(x2),round(y2));
  71.   color=(a+b)/2+
  72.     ((((float)random(32000))/32000.0)-0.5)*F*sqrt(pow(x2-x1,2)+pow(y2-y1,2));
  73.   a=(int)(color-0.5);
  74.   if (a<1) a=1;
  75.   if (a>192) a=191;
  76.   setpixel(round(x1+(x2-x1)/2),round(y1+(y2-y1)/2),a);
  77. }
  78.  
  79. //----------------------------------------------------------------------------
  80.  
  81. void subdivide(float topx, float topy, float height, float width,
  82.                unsigned char flag)
  83. {
  84.   if(width<1.0 && height<1.0) return;
  85.   adjust(topx,topy,topx+xadj,topy+height);
  86.   adjust(topx,topy,topx-xadj,topy+height);
  87.   adjust(topx-xadj,topy+height,topx+xadj,topy+height);
  88.   subdivide(topx,topy,height/2,width/2,flag);
  89.   subdivide(topx-xadj/2,topy+yadj,height/2,width/2,flag);
  90.   subdivide(topx+xadj/2,topy+yadj,height/2,width/2,flag);
  91.   subdivide(topx,topy+height,-height/2,width/2,flag^1);
  92. }
  93.  
  94. //----------------------------------------------------------------------------
  95.  
  96. void rotatepalette(unsigned char p[][3])
  97. {
  98.   movmem(p[1],p[2],192*3);
  99.   p[1][0]=p[193][0];
  100.   p[1][1]=p[193][1];
  101.   p[1][2]=p[193][2];
  102.   setpalette(&p[0][0]);
  103. }
  104.  
  105. //===========================================================================
  106.  
  107. void main()
  108. {
  109.   unsigned char p[256][3];
  110.   int x,y;
  111.   float width,height;
  112.  
  113.   r.r_ax=0x13;
  114.   intr(0x10,&r);
  115.  
  116.   p[0][0]=0;
  117.   p[0][1]=0;
  118.   p[0][2]=0;
  119.  
  120.   for (x=0;x<64;x++)
  121.   {
  122.     p[x+1][0]=0;      p[x+1][1]=x;      p[x+1][2]=63-x;
  123.     p[x+65][0]=x;     p[x+65][1]=63-x;  p[x+65][2]=0;
  124.     p[x+129][0]=63-x; p[x+129][1]=0;    p[x+129][2]=x;
  125.   }
  126.   setpalette(&p[0][0]);
  127.  
  128.   randomize();
  129.   setpixel(160, 0,          0);
  130.   setpixel(160+INITWIDTH/2, INITHEIGHT, 64);
  131.   setpixel(160-INITWIDTH/2,  INITHEIGHT, 128);
  132.  
  133.   subdivide(160,0,INITHEIGHT,INITWIDTH,0);
  134.   while(!kbhit())
  135.     rotatepalette(p);
  136.   getch();
  137.   r.r_ax=0x13;
  138.   intr(0x10,&r);
  139. }
  140.  
  141.